using System.Collections.Generic;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Controls;
using SmartBoard.Commands;
using SmartBoard.Ink.PenCreator;

namespace SmartBoard.Ink
{
    public class InkRendererCommandBinder : CommandBinderBase
    {
        #region Private Fields
        private readonly InkRenderer _renderer;
        #endregion

        #region Ctor
        public InkRendererCommandBinder(InkRenderer renderer) : base(renderer)
        {
            _renderer = renderer;
            _bindings = new List<CommandBinding>
                            {
                                Cut(),
                                Copy(),
                                Paste(),
                                Delete(),
                                SelectAll(),
                                ClearInk(),
                                Lock(),
                                SelectEditingMode(),
                                SelectPen(),
                            };
        }
        #endregion

        #region Private Methods
        private CommandBinding Copy()
        {
            return new CommandBinding(ApplicationCommands.Copy,
                                      (s, e) =>
                                      {
                                          _renderer.CopySelection();
                                      },
                                      (s, e) =>
                                      {
                                          e.CanExecute = !_renderer.IsLocked && _renderer.GetSelectedStrokes().Count != 0;
                                          e.Handled = true;
                                      });
        }

        private CommandBinding Paste()
        {
            return new CommandBinding(ApplicationCommands.Paste,
                                      (s, e) =>
                                      {
                                          _renderer.Paste();
                                      },
                                      (s, e) =>
                                      {
                                          e.CanExecute = _renderer.CanPaste() && !_renderer.IsLocked;
                                          e.Handled = true;
                                      });
        }


        private CommandBinding Delete()
        {
            return new CommandBinding(ApplicationCommands.Delete,
                                      (s, e) =>
                                      {
                                          if (_renderer.GetSelectedStrokes().Count > 0)
                                          {
                                              foreach (Stroke strk in _renderer.GetSelectedStrokes())
                                                  _renderer.Strokes.Remove(strk);
                                          }
                                      },
                                      (s, e) =>
                                      {
                                          e.CanExecute = _renderer.GetSelectedStrokes().Count > 0 && !_renderer.IsLocked;
                                          e.Handled = true;
                                      });
        }

        private CommandBinding ClearInk()
        {
            return new CommandBinding(InkRendererCommands.ClearInk,
                                      (s, e) =>
                                      {
                                          _renderer.Strokes.Clear();
                                      },
                                      (s, e) =>
                                      {
                                          e.CanExecute = !_renderer.IsLocked;
                                          e.Handled = true;
                                      });
        }


        private CommandBinding SelectAll()
        {
            return new CommandBinding(ApplicationCommands.SelectAll,
                                      (s, e) =>
                                      {
                                          _renderer.Select(_renderer.Strokes);
                                      },
                                      (s, e) =>
                                      {
                                          e.CanExecute = _renderer.Strokes.Count > 0;
                                          e.Handled = true;
                                      });
        }

        private CommandBinding Cut()
        {
            return new CommandBinding(ApplicationCommands.Cut,
                                      (s, e) =>
                                          {
                                              _renderer.CutSelection();
                                          },
                                      (s, e) =>
                                          {
                                              e.CanExecute = _renderer.GetSelectedStrokes().Count > 0 && !_renderer.IsLocked;
                                              e.Handled = true;
                                          });
        }

        private CommandBinding Lock()
        {
            return new CommandBinding(InkRendererCommands.Lock,
                                      (s, e) =>
                                      {
                                          var canLock = (bool) e.Parameter;
                                          _renderer.IsLocked = canLock;
                                          _renderer.EditingMode = _renderer.IsLocked ? InkCanvasEditingMode.None : InkCanvasEditingMode.Ink;
                                      });
        }

        private CommandBinding SelectEditingMode()
        {
            return new CommandBinding(InkRendererCommands.SelectEditingMode,
                                      (s, e) =>
                                          {
                                              var editingMode = (InkCanvasEditingMode) e.Parameter;
                                              _renderer.EditingMode = editingMode;
                                          },
                                      (s, e) =>
                                          {
                                              e.CanExecute = !_renderer.IsLocked;
                                          });
        }

        private CommandBinding SelectPen()
        {
            return new CommandBinding(InkRendererCommands.SelectPen,
                                      (s, e) =>
                                          {
                                              var commandDialog = new PenCreatorDialog
                                                                      {
                                                                          DrawingAttributes = _renderer.DefaultDrawingAttributes
                                                                      };
                                              if (commandDialog.ShowDialog() == true)
                                              {
                                                  _renderer.DefaultDrawingAttributes = commandDialog.DrawingAttributes;
                                              }
                                          }
                );
        }
        #endregion
    }
}